package com.itfreer.gather.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseRestService;
import com.itfreer.form.api.BaseService;
import com.itfreer.gather.entity.NoticeEntity;
import com.itfreer.jwt.JwtTicket;
import com.itfreer.jwt.JwtTicketFactory;
import com.itfreer.power.entity.user.VUserInfoEntity;
import com.itfreer.utils.json.JsonUtils;

/**
 * 定义移动端工程Rest服务接口
 */
@Component
@Path("/gisnotice/")
public class RestNoticeService extends BaseRestService<NoticeEntity> {

	@Autowired
	private NoticeService service;
	@Autowired
	private JwtTicketFactory jwtTicketFactory;

	@Override
	protected BaseService<NoticeEntity> getService() {
		return service;
	}
	
	@Override
	protected String getServiceName() {
		return "gisnotice";
	}
	
	/**
	 * 添加实体
	 * 
	 * @param entity
	 *            添加实体
	 * @return
	 * @throws IOException
	 */
	@Override
	@POST
	@Path("/add")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public NoticeEntity add(@Context HttpServletRequest request, String data) throws IOException {
		JwtTicket jwtTicket = jwtTicketFactory.getTicket(request);
		VUserInfoEntity userEntity = jwtTicket != null ? (VUserInfoEntity) jwtTicket.getAttribute().get("user") : null;
		HashMap<String, Object> args = getRestParam(data);
		String entityJson = null;
		if (args != null && args.containsKey("entity") && args.get("entity") != null) {
			entityJson = args.get("entity").toString();
		}
		NoticeEntity entity = (NoticeEntity) JsonUtils.toBean(entityJson, NoticeEntity.class);
		entity.setPublishDate(new Date());
		if(userEntity!=null) {
			entity.setPublishUserID(userEntity.getUserid());
			entity.setPublishUserName(userEntity.getUserName());
		}
		return getService().add(entity);
	}
}
